//Clean Daily Return Data + Add Compute Daily DGTW Returns
use ../src/stock_daily_ret.dta, replace

// Cleaning Dates and Stock Codes
destring stkcd, gen(code) 
gen date2 = ym(year, month) // month for stock characteristics

// Merging on Stock Characteristics
merge m:1 code date2 using ../src/assignment_m
drop if _m==2
drop _merge 

// DGTW Returns
gen t1 = ret*float_cap_1
bysort date q_cap q_bm q_mom: egen t2=sum(float_cap_1) 
bysort date q_cap q_bm q_mom: egen t3=sum(t1) 
gen m = t3/t2	
replace m=. if q_cap==. | q_bm==. | q_mom==. 
gen ret_dgtw = ret - m 
drop m t1 t2 t3

// Value-weighted market return *
gen t1 = ret*float_cap_1
bysort date: egen t2=sum(float_cap_1) 
bysort date: egen t3=sum(t1) 
gen m = t3/t2
gen ret_mkt = ret - m 
drop m t1 t2 t3

// Keeping Relevant Variables
keep code date ret* price  float_cap volume
 
// Saving Daily Stock Data
save ../dta/stock_daily, replace 
